﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace ExceptionFinallyThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            PerformanceDemo.RunTest();
            

            return;

            Thread t1 = new Thread(new ThreadStart(DoSomething));
            t1.Start();
            Thread.Sleep(3000);

            try
            {
                throw new Exception();
            }
            finally
            {
                t1.Abort();
            }
        }

        static void DoSomething()
        {
            using (TextWriter wr = new StreamWriter("out.txt"))
            {
                for (int i = 0; i < 20; i++)
                {
                    Thread.Sleep(1000);
                    wr.WriteLine(i);
                }
            }
        }


        class PerformanceDemo
        {
            private const int BUFFER_SIZE = 500 * 1024;

            public static void RunTest()
            {

                DateTime startTime = DateTime.Now;
                TimeSpan duration;

                startTime = DateTime.Now;
                Console.WriteLine("\nEffective calculation started...");
                int[] buff = new int[BUFFER_SIZE];
                for (int count = 0; count < 1000; count++)
                {
                    for (int bufIndex = 0; bufIndex < buff.Length; bufIndex++)
                    {
                        buff[bufIndex] = bufIndex;
                    }
                }
                duration = DateTime.Now - startTime;
                Console.WriteLine("Effective calculation finished.");
                Console.WriteLine("Effective calculation took {0} sec.", duration.TotalSeconds);


                Console.ReadKey();

                startTime = DateTime.Now;
                Console.WriteLine("Ineffective calculation started...");
                for (int count = 0; count < 1000; count++)
                {
                    int[] buffer = new int[BUFFER_SIZE];
                    for (int bufIndex = 0; bufIndex < buffer.Length; bufIndex++)
                    {
                        buffer[bufIndex] = bufIndex;
                    }
                }
                duration = DateTime.Now - startTime;
                Console.WriteLine("Ineffective calculation finished.");
                Console.WriteLine("Ineffective calculation took {0} sec.", duration.TotalSeconds);
                

                Console.ReadKey();
            }
        }

    }
}
